package com.matt.controller.api.web;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.controller.po.ProductPO;
import com.matt.domain.Product;

import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.StatusResponse;
import com.matt.foundation.query.ProductQuery;
import com.matt.remoteCall.SsoCallService;
import com.matt.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.*;

import static com.matt.foundation.constant.CommonConstant.TICKET;

/**
 * *******************************
 * Created by Ron
 * from com.matt
 * on 2018/6/13 0013.
 * *******************************
 */
@RestController
@RequestMapping("/web/product")
@Api(tags = "CRM - 产品")
public class ProductController {
    @Autowired
    private ProductService productService;

    @Autowired
    private SsoCallService ssoCallService;

    @GetMapping(value = "/page")
    @ApiOperation(value = "产品分页", httpMethod = "GET")
    public StatusResponse page(@RequestHeader(TICKET) String ticket, ProductQuery query) {

        StaffDTO staff = ssoCallService.getStaffDTO(ticket);

        String areaId = staff.getAreaId();

        query.setAreaId(areaId);

        PageRequest pageRequest =  PageRequest.of(query.getPageNum(), query.getPageSize());

        Page<Product> page = productService.getPages(query, pageRequest);

        return StatusResponse.success(page);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "增加产品", httpMethod = "POST")
    public StatusResponse add(@RequestHeader(TICKET) String ticket,
                              ProductPO param) {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String areaId = staff.getAreaId();

        param.setAreaId(areaId);

        return StatusResponse.success(productService.add(staff.getId(), param));
    }

    @GetMapping(value = "/detail")
    @ApiOperation(value = "产品详情", httpMethod = "GET")
    public StatusResponse detail(@RequestHeader(TICKET) String ticket, String id) {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String areaId = staff.getAreaId();
        return StatusResponse.success(productService.findOne(id, areaId));
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "更新产品详情", httpMethod = "POST")
    public StatusResponse update(@RequestHeader(TICKET) String ticket, ProductPO param) {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String areaId = staff.getAreaId();

        param.setAreaId(areaId);

        return StatusResponse.success(productService.update(staff.getId(), param));
    }

    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    @ApiOperation(value = "删除产品", httpMethod = "DELETE")
    public StatusResponse delete(@RequestHeader(TICKET) String ticket, String id) {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String areaId = staff.getAreaId();

        return StatusResponse.success(productService.delete(id, areaId));
    }

    @GetMapping(value = "product_type")
    @ApiOperation(value = "产品类型列表", httpMethod = "GET")
    public StatusResponse productType(@RequestHeader(TICKET) String ticket) {
        StaffDTO staff = ssoCallService.getStaffDTO(ticket);
        String areaId = staff.getAreaId();
        return StatusResponse.success(productService.getTypeByAreaId(areaId));
    }


    @ApiOperation(value = "审核一条记录")
    @PostMapping("/audit")
    public StatusResponse audit(@RequestHeader(TICKET) String ticket,
                                                       String id, Integer audited, @RequestParam(required = false) String auditContent
    ){

        boolean permited = ssoCallService.checkStaffPermission(ticket, new String[]{"finance::audit::all"});

        if (!permited){
            return StatusResponse.error(ErrorCode.ROLE_NOT_EXIST, "权限错误");
        }

        StaffDTO staff = ssoCallService.getStaffDTO(ticket);


        if (StringUtils.isBlank(id)){
            return StatusResponse.error(ErrorCode.PARAM_CAN_NOT_BLANK, "id 必填");
        }

        if (audited != 1 && audited != 2){
            return StatusResponse.error(ErrorCode.PARAM_CAN_NOT_BLANK, "审核状态不对");
        }

        Product ret = productService.findOne(id, staff.getAreaId());

        if (ret == null){
            return StatusResponse.error(ErrorCode.PARAM_CAN_NOT_BLANK, "对象不存在");
        }

        ret.setAudited(audited);

        ret = productService.update(ret);

        return StatusResponse.success(ret);
    }
}
